﻿using MyPlan.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Easy4net.DBUtility;
using MyPlan.Utils;
using Easy4net.Common;

namespace MyPlan.Controllers
{
    public class UserController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<User> Get()
        {
            return null;
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        [HttpPost]
        public Response Login([FromBody]User user)
        {
            Response response = Response.NewResponse();

            string sql = "SELECT * FROM USER WHERE account = ?account AND password = ?password limit 1";
            ParamMap param = ParamMap.newMap();
            param.setParameter("account", user.Account);
            param.setParameter("password", user.Password);

            DBHelper db = DBHelper.getInstance();
            List<User> users = db.FindBySql<User>(sql, param);

            if (users.Count == 0)
            {
                return response.ACCOUNT_PASSWORD_ERROR();
            }

            return response.Ok(users[0]);        
        }




        // POST api/<controller>
        public User Post([FromBody]User user)
        {
            DBHelper db = DBHelper.getInstance();
            db.Save<User>(user);

            return user;
        }

        // PUT api/<controller>/5
        public User Put(int id, [FromBody]User user)
        {
            DBHelper db = DBHelper.getInstance();
            user.Id = id;
            db.Update<User>(user);

            return user;
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}